Thread: [Jumping into C++] Need help with Chapter 7, problem 1

  1. #1
    Registered User
    Join Date
    Jun 2016
    Posts
    40

    [Jumping into C++] Need help with Chapter 7, problem 1

    So, I was strengthening my C++ foundation with this book, reading each chapter and doing each practice problem (even when I already knew the material). I'm currently on chapter 7, practice problem 1, which is:

    Implement the source code that turns numbers into English text.

    What I've tried thus far:
    - See how I could do it with all the material up to that chapter. Seems I couldn't, so I relied on my overall knowledge (and some tutorials).
    - Was stumped on how to begin coding it, so I spent a few days pseudocoding and brainstorming before I found an algorithm I thought would work.
    - Spent about a week coding in that algorithm, changing some things (since the functions got longer than I expected). Took this long due to all the debugging, and making helper functions.

    Now, I managed to make a functional program, but the thing is, it doesn't translate like it should. I'm not sure where I went wrong, but after spending a week, I thought I should post here to get some insight.

    Here's the (long and ugly) source: show at bpaste

    I'd very much appreciate help with this

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Implement the source code that turns numbers into English text.
    I'm assuming this means translate "17" into "seventeen".

    I would say, start slow with this. Use a pen and paper and write out a bunch of sample data and look for patterns. For example, if we have a two digit number and it begins with a one, chances are it's going to be "teen". It could also be eleven or twelve.

    But before that, write a function that can just handle 0 through 9. That should be simple enough. You can even use a giant switch-statement.

    When it comes to larger digits, you're probably going to have to look at the number of digits first because that will help determine the grammar of the English representation. That's kind of what we're doing with "17". We know it's two digits and that it starts with a 1. If the next digit is a 1 we return "eleven". It's it' a "2" we return "twelve". If it's a "3" we return "thirteen".

    Man, this sounds hard.

  3. #3
    Registered User taazz's Avatar
    Join Date
    May 2016
    Posts
    50
    That was scary. Sorry I do not have the time to read all the code posted but I have to say you missed the target by a mile. Just two hints, you do not need to convert the number to string at all and you need a couple of arrays of strings for a fast conversion. I will take a detail look on your code at a later time and I'll be back with more details hopefully. If you have known numbers that it fails it would be helpful to post them too.

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by MutantJohn View Post
    Man, this sounds hard.
    It's actually easy. Give it a try!

  5. #5
    Registered User
    Join Date
    Jun 2016
    Posts
    40
    @MutantJohn: That part about finding patterns was explained in the chapter, and in the case of this problem, the pattern-finding was done for me. Look in the chapter, and you'll see that the pattern-finding for this was spelled out.

    And based on that, I made my algorithm.

    @taaz: I don't blame you xD Also, I'll see what I can do to figure it out with your hint.

  6. #6
    Registered User taazz's Avatar
    Join Date
    May 2016
    Posts
    50
    Quote Originally Posted by Tesp View Post
    @MutantJohn: That part about finding patterns was explained in the chapter, and in the case of this problem, the pattern-finding was done for me. Look in the chapter, and you'll see that the pattern-finding for this was spelled out.
    And based on that, I made my algorithm.
    I just downloaded the book and took a quick look on the problem and as far as I can see the methodology was spelled out for you a number of samples of the patterns were given but they are neither complete nor accurate, you need to work on it your self for example how many "unique" numbers there are in English before you start adding postfixes for the "order of magnitute" (lacking of a better word).
    Quote Originally Posted by Tesp View Post
    @taaz: I don't blame you xD Also, I'll see what I can do to figure it out with your hint.
    You have already used the methodology for my 1st hint on the problem with the primes in the same book.

  7. #7
    Registered User
    Join Date
    Jun 2016
    Posts
    40
    So, I spent some more time on it, and I'm still stumped.

    What I tried this time:

    - Try to think of an algorithm that can turn a number from 0 to a cap of 999,999,999 into english text, all without using string conversions. It would iterate through each number from zero to that cap, using some basic arithmetic operations. These operations would help fill a string array with the english translations of each iterated integer, each integer corresponding to its index in that array. However, the best I could think of was one that'd work for numbers from 0-999, which is far from my initial goal.

    - Go back to my previous program, and manually trace the logic based on a sample input of 123. I had no idea how 123 was translated as "one hundred twenty three". I would have used the Code Blocks or Netbeans debugger, but for some reason, they dont let me see the contents of vectors. That is, except for the first element added. That annoyance pretty much rendered debuggers useless for this problem (I need to know how the contents got changed around!), hence why I had to manually trace the logic.

    - I forgot to mention this when making the OP, but I did try using the debuggers before asking here for help. As the above point explains, they didn't really help.

    So, yeah. Any other advice? I'll spend another couple of days on this, seeing if I can stumble upon some solution in the meantime.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    - Try to think of an algorithm that can turn a number from 0 to a cap of 999,999,999 into english text, all without using string conversions. It would iterate through each number from zero to that cap, using some basic arithmetic operations. These operations would help fill a string array with the english translations of each iterated integer, each integer corresponding to its index in that array. However, the best I could think of was one that'd work for numbers from 0-999, which is far from my initial goal.
    A lookup table would take far too much memory if you stored all of the numbers.

    It is better to break up the number into groups of three. Say I gave you a small number 123. There are three small place values in this number that you should be able to divide to get to digit form. 1=one hundred, 2=twenty 3=three.

    Once you master this, then you can take your code that explains this group to much larger numbers like 31,415,930. You will notice that other, larger place values are also groups of three: "thirty one million four hundred fifteen thousand nine hundred thirty." So it is all about having the right words for place value after the group of three.

  9. #9
    Registered User
    Join Date
    Mar 2016
    Posts
    203
    OP: if you are open to looking at what's available out there, you might wish to take a look at the following link. Unless of course you want to get to the end result on your own and I respect that:

    c++ - Convert number to words - Code Review Stack Exchange

  10. #10
    Registered User
    Join Date
    Jun 2016
    Posts
    40
    Quote Originally Posted by whiteflags View Post
    A lookup table would take far too much memory if you stored all of the numbers.

    It is better to break up the number into groups of three. Say I gave you a small number 123. There are three small place values in this number that you should be able to divide to get to digit form. 1=one hundred, 2=twenty 3=three.

    Once you master this, then you can take your code that explains this group to much larger numbers like 31,415,930. You will notice that other, larger place values are also groups of three: "thirty one million four hundred fifteen thousand nine hundred thirty." So it is all about having the right words for place value after the group of three.
    I'll try that. When I finish the code that converts numbers up to three digits long, I'll post it here before tackling the initial goal.

    Quote Originally Posted by sean_cantab View Post
    OP: if you are open to looking at what's available out there, you might wish to take a look at the following link. Unless of course you want to get to the end result on your own and I respect that:

    c++ - Convert number to words - Code Review Stack Exchange
    Those code examples don't quite solve my problem (I'm going for 9 digits, not 3), but I'll reverse engineer those code samples and see if that'll help.


    Thanks to the both of you

  11. #11
    Registered User
    Join Date
    Mar 2016
    Posts
    203
    Scroll down, it goes up to 999,999,999

  12. #12
    Registered User taazz's Avatar
    Join Date
    May 2016
    Posts
    50
    Quote Originally Posted by Tesp View Post
    So, I spent some more time on it, and I'm still stumped.

    What I tried this time:

    - Try to think of an algorithm that can turn a number from 0 to a cap of 999,999,999 into english text, all without using string conversions. It would iterate through each number from zero to that cap, using some basic arithmetic operations. These operations would help fill a string array with the english translations of each iterated integer, each integer corresponding to its index in that array. However, the best I could think of was one that'd work for numbers from 0-999, which is far from my initial goal.

    - Go back to my previous program, and manually trace the logic based on a sample input of 123. I had no idea how 123 was translated as "one hundred twenty three". I would have used the Code Blocks or Netbeans debugger, but for some reason, they dont let me see the contents of vectors. That is, except for the first element added. That annoyance pretty much rendered debuggers useless for this problem (I need to know how the contents got changed around!), hence why I had to manually trace the logic.

    - I forgot to mention this when making the OP, but I did try using the debuggers before asking here for help. As the above point explains, they didn't really help.

    So, yeah. Any other advice? I'll spend another couple of days on this, seeing if I can stumble upon some solution in the meantime.
    OK lets forget for a second the numbers, how about the words, what are the smaller number of string constants, required to be able to combine them in all the numbers up to 100?
    Have you thought through that already?
    for example the number from 1 to 20 are all unique words, there is not a single repetition. what else is missing?

    To get you started let me show you the way to break a number down to its parts.

    You start with a number (lets do 3 digits) 539, now to get the first digit which is the smallest number aka the right most digit, you use modulus (known as mod) 10, so in order to get 9 out of 539 you calculate 539 mod 1, (if I'm not mistaken the character % is used to calculate mod in c/c++) and you get 9 as a result. To remove its value from the existing number leaving you with 53 you use an integer division eg 539 / 10 which it will return 53 as a result. Keep on going keeping a order some where (like 1 for 539, 2 for 53, 3 for 5 etc) while you work and you have all the information needed to solve your problem.

    Can you work your way through this now?
    Better now?
    Last edited by taazz; 06-29-2016 at 12:03 PM.

  13. #13
    Registered User
    Join Date
    Jun 2016
    Posts
    40
    Quote Originally Posted by sean_cantab View Post
    Scroll down, it goes up to 999,999,999
    Oh, I see it now! Though I'll see if I can solve the problem my own way, first; if I can't, I'll reverse engineer that example so I can at least get how it works.

    @taazz: Could you please fix the grammar in your post? I really can't understand what you're trying to say, there ^^;

  14. #14
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Actually, the grammar is fine. You're just having trouble visualizing the algorithm.

  15. #15
    Registered User
    Join Date
    Jun 2016
    Posts
    40
    Quote Originally Posted by MutantJohn View Post
    Actually, the grammar is fine. You're just having trouble visualizing the algorithm.
    I have no shame in admitting I'm having trouble with the algorithm. Still, before he edited his post, the grammar was too flawed for me to understand.


    Quote Originally Posted by taazz View Post
    OK lets forget for a second the numbers, how about the words, what are the smaller number of string constants, required to be able to combine them in all the numbers up to 100?
    Have you thought through that already?
    I have, though not for the numbers up to 100.

    Quote Originally Posted by taazz View Post
    for example the number from 1 to 20 are all unique words, there is not a single repetition. what else is missing?
    I thought the only unique words were for the numbers 10-19, since the others single-to-double-digit numbers are easier to format.

    Quote Originally Posted by taazz View Post
    To get you started let me show you the way to break a number down to its parts.

    You start with a number (lets do 3 digits) 539, now to get the first digit which is the smallest number aka the right most digit, you use modulus (known as mod) 10, so in order to get 9 out of 539 you calculate 539 mod 1, (if I'm not mistaken the character % is used to calculate mod in c/c++) and you get 9 as a result. To remove its value from the existing number leaving you with 53 you use an integer division eg 539 / 10 which it will return 53 as a result. Keep on going keeping a order some where (like 1 for 539, 2 for 53, 3 for 5 etc) while you work and you have all the information needed to solve your problem.

    Can you work your way through this now?
    Better now?
    I think I get what you mean. In fact, I did have integer division and modulus in the pseudocode for my second attempt, but got stumped in terms of how to use it for numbers with more than 3 digits. I'll try reformulating my algorithm with this, and I'll report the results.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Jumping Into C++ Chapter 14 Problem 4
    By mbartholomew in forum C++ Programming
    Replies: 3
    Last Post: 12-28-2014, 03:10 PM
  2. Jumping Into C++: Chapter 14 Problem 1
    By programmerafael in forum C++ Programming
    Replies: 2
    Last Post: 07-21-2014, 12:28 AM
  3. jumping into c++ chapter 5 problem 7
    By etricity in forum C++ Programming
    Replies: 4
    Last Post: 04-06-2014, 11:23 PM
  4. Jumping Into C++ - Chapter 8 Problem[3]
    By Mohamed Adel in forum C++ Programming
    Replies: 3
    Last Post: 08-28-2013, 09:14 AM
  5. Jumping To C++ - Chapter 8 Problem
    By Mohamed Adel in forum C++ Programming
    Replies: 4
    Last Post: 08-27-2013, 01:02 PM

Tags for this Thread